package com.huishen.edrivenew.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import butterknife.InjectView;

import com.huishen.edrivenew.AppContext;
import com.huishen.edrivenew.R;
import com.huishen.edrivenew.bean.CoaLessonInfoBean;
import com.huishen.edrivenew.bean.CoachBean;
import com.huishen.edrivenew.bean.CoachListBean;
import com.huishen.edrivenew.bean.LessInfoBean;
import com.huishen.edrivenew.bean.LessonTimeBean;
import com.huishen.edrivenew.bean.ListLessonTimeBean;
import com.huishen.edrivenew.net.BitmapManager;
import com.huishen.edrivenew.util.StringUtils;
import com.huishen.edrivenew.util.TLog;
import com.huishen.edrivenew.widget.CircleImageView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 教练列表约课界面下拉列表适配器
 *
 */
public class AppoCoachListApdater extends BaseExpandableListAdapter {

    private Context mContext;
    private LayoutInflater mInflater = null;
    private TimeClassSchoolListener listener;

    private CoachListBean coachList;

    private List<CoachBean> rows = new ArrayList<CoachBean>();
    private Map<Integer, ListLessonTimeBean> childMap = new HashMap<Integer, ListLessonTimeBean>();

    BitmapManager bmpManager;
    

    public AppoCoachListApdater(Context ctx, CoachListBean coachList,
                                TimeClassSchoolListener listener) {
        mContext = ctx;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.listener = listener;
        this.coachList = coachList;
        
        bmpManager = new BitmapManager(BitmapFactory.decodeResource(AppContext.getInstance().getResources(), 
            R.drawable.photo_bg_t));
        
        setData();
    }



    public void setData() {
        this.rows.clear();
        this.rows.addAll(coachList.rows);
    }
    
    public void setChild(int index, ListLessonTimeBean timeBean) {
        childMap.put(index, timeBean);
    }
    
    public void claenChild() {
        childMap.clear();
    }

    @Override
    public ListLessonTimeBean getChild(int groupPosition, int childPosition) {
        try {
            return childMap.get(groupPosition);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }


    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {

        GroupViewHolder group = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_appolist_sub, null);
            group = new GroupViewHolder();
            group.imageView = (CircleImageView) convertView.findViewById(R.id.f_appoint_photo);
            group.subName = (TextView) convertView.findViewById(R.id.f_appoint_coachname);
            group.carname = (TextView) convertView.findViewById(R.id.f_appoint_coachcar);
            group.carNumber = (TextView) convertView.findViewById(R.id.f_appoint_carnum);
            group.stars = (RatingBar) convertView.findViewById(R.id.f_app_coach_ratingbar);

            convertView.setTag(group);
        } else {
            group = (GroupViewHolder) convertView.getTag();
        }

        // ----------------添加列表数据------------------
        CoachBean gBean = rows.get(groupPosition);
        group.subName.setText(gBean.name);
        group.carname.setText(gBean.carBrand);
        group.carNumber.setText(gBean.carNum);
        group.stars.setRating(gBean.score);
        
        if(!StringUtils.isEmpty(gBean.path)) {
            bmpManager.loadBitmap(gBean.path, group.imageView);
        }

        // imageview
        return convertView;
    }

    //

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        ChildViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_appolist_class, null);
            holder = new ChildViewHolder();

            holder.itemList = new ArrayList<AppoCoachListApdater.ChildHolderItem>();
            for (int i = 0; i < 12; i++) {
                ChildHolderItem item = new ChildHolderItem();
                if (i == 0) {
                    item.itemView = convertView.findViewById(R.id.time01);
                } else {
                    item.itemView = convertView.findViewById(R.id.time01 + i);
                }
                item.classTime = (TextView) item.itemView.findViewById(R.id.item_class_time);
                item.classStatus = (TextView) item.itemView.findViewById(R.id.item_class_status);
                item.classMoney = (TextView) item.itemView.findViewById(R.id.item_class_money);
                item.imageBg = (ImageButton) item.itemView.findViewById(R.id.ca_bg);

                holder.itemList.add(i, item);
            }
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time01) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time02) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time03) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time04) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time05) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time06) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time07) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time08) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time09) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time10) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time11) ;
            // holder.itemList.get(0).itemView = convertView.findViewById(R.id.time12) ;
            convertView.setTag(holder);
        } else {
            holder = (ChildViewHolder) convertView.getTag();
        }
        ListLessonTimeBean bean = childMap.get(groupPosition);
        if (bean != null) {
            for (int i = 0; i < 12; i++) {
                if (bean.lessonTime == null || i >= bean.lessonTime.size()) {
                    holder.itemList.get(i).itemView.setVisibility(View.INVISIBLE);
                } else {
                    // 有数据
                    holder.itemList.get(i).itemView.setVisibility(View.VISIBLE);
                    final LessonTimeBean timeBean = bean.lessonTime.get(i);

                    holder.itemList.get(i).classTime.setText(timeBean.duringDate + "");
                    holder.itemList.get(i).classMoney.setText("￥" + timeBean.money);

                    if (timeBean.status == 1) {// 已约
                        holder.itemList.get(i).classStatus.setText("取消");
                        holder.itemList.get(i).imageBg.setBackgroundResource(R.drawable.day_exists);
                        holder.itemList.get(i).imageBg.setEnabled(true);
                    } else if (timeBean.status == 2) { // 已满
                        holder.itemList.get(i).classStatus.setText("已满");
                        holder.itemList.get(i).imageBg.setBackgroundResource(R.drawable.day_full);
                        holder.itemList.get(i).imageBg.setEnabled(false);
                    } else if (timeBean.status == 0) {// 可约
                        holder.itemList.get(i).classStatus.setText("预约");
                        holder.itemList.get(i).imageBg.setBackgroundResource(R.drawable.day_ok);
                        holder.itemList.get(i).imageBg.setEnabled(true);
                    } else {
                        // 3
                        holder.itemList.get(i).classStatus.setText("不可预约");
                        holder.itemList.get(i).imageBg.setBackgroundResource(R.drawable.day_full);
                        holder.itemList.get(i).imageBg.setEnabled(false);
                    }
                    
                    holder.itemList.get(i).imageBg.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            listener.setOnClick(timeBean, rows.get(groupPosition), timeBean.status);
                        }
                    });

                }
            }

        }

        return convertView;
    }

    private class GroupViewHolder {
        CircleImageView imageView;
        TextView subName;
        TextView carname;
        TextView carNumber;
        RatingBar stars;
    }


    @Override
    public int getGroupCount() {
        return rows.size();
    }



    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }



    @Override
    public Object getGroup(int groupPosition) {
        return rows.get(groupPosition);
    }



    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }



    @Override
    public boolean hasStableIds() {
        return false;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }


    // TODO 临时做法.
    static class ChildViewHolder {
        List<ChildHolderItem> itemList;
    }

    class ChildHolderItem {
        View itemView;

        ImageButton imageBg;
        TextView classTime;
        TextView classStatus;
        TextView classMoney;
    }



}
